<template>
  <div>
    <van-picker title="标题" show-toolbar :columns="columns" @confirm="onConfirm" @cancel="onCancel" @change="onChange" :default-index="value" />
  </div>
</template>

<script>
import { updataUsersAPI } from '@/api/User.js'
export default {
  name: 'updataGener',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      columns: ['男', '女'],
      localGener: this.value //修改的数据
    }
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, //禁止背景点击
        duration: 0
      })
      try {
        await updataUsersAPI({
          gender: this.localGener
        })
        // 更新视图
        this.$emit('input', this.localGener)
        //关闭弹出层
        this.$emit('close')
        //提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('修改性别失败，请稍后重试！')
        console.log(err)
      }
    },
    onChange(picker, value, index) {
      this.localGener = index
    },
    onCancel() {
      // 点击取消，执行的操作
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped></style>
